package com.smartrobot.smartrobot.util;

import android.text.format.Time;
import android.util.Log;

import com.smartrobot.smartrobot.Common.Common;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by Administrator on 2014/12/24.
 */
public class TimeUtil {

    static Calendar calendar = Calendar.getInstance();

    public static String getCurrentTimeFromat(){
        Time time = new Time();
        time.setToNow();

        String year = String.valueOf(time.year);
        String month;
        String day;
        String hour;
        String minutes;

        if(time.month < 10)
            month = "0"+String.valueOf(time.month);
        else
            month = String.valueOf(time.month);
        if(time.monthDay < 10)
            day = "0"+String.valueOf(time.monthDay);
        else
            day = String.valueOf(time.monthDay);
        if(time.hour < 10)
            hour = "0"+String.valueOf(time.hour);
        else
            hour =  String.valueOf(time.hour);
        if(time.minute < 10)
            minutes = "0"+String.valueOf(time.minute);
        else
            minutes = String.valueOf(time.minute);

        return year+month+day+hour+minutes;


    }

    public static String getToday(){
        Time time = new Time();
        time.setToNow();

        String month;
        String day;

            month = String.valueOf(time.month);
            day = String.valueOf(time.monthDay);


        return month+"-"+day;

    }

    public static String getTomorrow(){
        Time time = new Time();
        time.setToNow();

        String month;
        String day;

        month = String.valueOf(time.month);
        day = String.valueOf(time.monthDay);


        return month+"-"+day;

    }

    // 提供“yyyy-mm-dd”形式的字符串到毫秒的转换

    public static long getMillis(String dateString) {

        String[] date = dateString.split("-");

        return getMillis(date[0], date[1], date[2]);

    }

// 根据输入的年、月、日，转换成毫秒表示的时间

    public static long getMillis(int year, int month, int day) {

        GregorianCalendar calendar = new GregorianCalendar(year, month, day);

        return calendar.getTimeInMillis();

    }

// 根据输入的年、月、日，转换成毫秒表示的时间

    public static long getMillis(String yearString, String monthString,

                                 String dayString) {

        int year = Integer.parseInt(yearString);

        int month = Integer.parseInt(monthString);

        int day = Integer.parseInt(dayString);

        return getMillis(year, month, day);

    }

    // 获得当前时间的毫秒表示

    public static long getNow() {

        GregorianCalendar now = new GregorianCalendar();

        return now.getTimeInMillis();

    }
    // 根据输入的毫秒数，获得日期字符串

    public static String getDate(long millis) {

        calendar.setTimeInMillis(millis);

        return DateFormat.getDateInstance().format(calendar.getTime());

    }

    // 根据输入的毫秒数，获得年份

    public static int getYear(long millis) {

        calendar.setTimeInMillis(millis);

        return calendar.get(Calendar.YEAR);

    }

// 根据输入的毫秒数，获得月份

    public static int getMonth(long millis) {

        calendar.setTimeInMillis(millis);

        return calendar.get(Calendar.MONTH);

    }

// 根据输入的毫秒数，获得日期

    public static int getDay(long millis) {

        calendar.setTimeInMillis(millis);

        return calendar.get(Calendar.DATE);

    }

// 根据输入的毫秒数，获得小时

    public static int getHour(long millis) {

        calendar.setTimeInMillis(millis);

        return calendar.get(Calendar.HOUR_OF_DAY);

    }

// 根据输入的毫秒数，获得分钟

    public static int getMinute(long millis) {

        calendar.setTimeInMillis(millis);

        return calendar.get(Calendar.MINUTE);

    }

// 根据输入的毫秒数，获得秒

    public static int getSecond(long millis) {

        calendar.setTimeInMillis(millis);

        return calendar.get(Calendar.SECOND);

    }


    // 获得今天日期

    public static String getTodayData() {

        return getDate(getNow());

    }

    // 获得今天日期

    public static String getTodayData(long time) {

        return getDate(time);

    }

    //获取网络时间
    public static long getNetTime(){
        try {
            URL url = new URL(Common.baseUrl);//取得资源对象
            URLConnection uc = null;//生成连接对象
            uc = url.openConnection();
            uc.setConnectTimeout(2000);
            uc.connect(); //发出连接
            long ld = uc.getDate(); //取得网站日期时间
            Date date = new Date(ld); //转换为标准时间对象
            //分别取得时间中的小时，分钟和秒，并输出

            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }

// 获得明天日期

    public static String getTomoData() {

// 86400000为一天的毫秒数

        return getDate(getNow() + 86400000);

    }

    public static String getTomoData(long time) {

// 86400000为一天的毫秒数

        return getDate(time + 86400000);

    }

// 获得后天日期

    public static String getTheDayData() {

        return getDate(getNow() + 86400000 + 86400000);

    }

    public static String getTheDayData(long time) {

        return getDate(time + 86400000 + 86400000);

    }



}
